package com.zc.cms.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zc.cms.entity.CrmBanner;
import com.zc.cms.service.CrmBannerService;
import com.zc.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author cong
 * @since 2022-11-30
 */
@RestController
@RequestMapping("/cms/bannerAdmin")
@CrossOrigin
public class BannerAdminController {
	@Autowired
	private CrmBannerService bannerService;

	@GetMapping("/pageBanner/{currentPage}/{size}")
	public R pageBanner(@PathVariable long currentPage, @PathVariable long size) {
		Page<CrmBanner> bannerPage = new Page<>(currentPage, size);
		IPage<CrmBanner> page = bannerService.page(bannerPage, null);
		List<CrmBanner> records = page.getRecords();
		long total = page.getTotal();
		return R.ok().data("records", records).data("total", total);
	}

	// 添加banner
	@PostMapping("/addBanner")
	public R addBanner(@RequestBody CrmBanner banner) {
		boolean save = bannerService.save(banner);
		return save ? R.ok() : R.error();
	}

	//删除banner
	@DeleteMapping("/deleteBanner/{id}")
	public R deleteBanner(@PathVariable String id) {
		boolean remove = bannerService.removeById(id);
		return remove ? R.ok() : R.error();
	}

	// 修改Banner
	@PostMapping("/updateBanner/{id}")
	public R updateBanner(@RequestBody CrmBanner banner) {
		boolean update = bannerService.updateById(banner);
		return update ? R.ok() : R.error();

	}

	@GetMapping("/getBannerById/{id}")
	public R getBannerById(@PathVariable String id) {
		CrmBanner banner = bannerService.getById(id);
		return R.ok().data("banner", banner);
	}

}

